package com.atguigu.controller;

import com.atguigu.feign.ProductFeignClient;
import com.atguigu.feign.SearchFeignClient;
import com.atguigu.result.RetVal;
import com.atguigu.search.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/8/30 17:31
 * @description：首页控制
 * @modified By：
 * @version: 0.0.1
 */
@Controller
public class IndexController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private SearchFeignClient searchFeignClient;

    @GetMapping({"/", "index.html"})
    public String index(Model model){
        RetVal retVal = productFeignClient.getIndexCategoryInfo();
        model.addAttribute("list", retVal.getData());
        return "index/index";
    }

    @GetMapping("search.html")
    public String searchProduct(SearchParam searchParam, Model model){
        RetVal<Map> retVal = searchFeignClient.searchProduct(searchParam);
        model.addAllAttributes(retVal.getData());
        model.addAttribute("searchParam",searchParam);
        //传入拼接后的url
        String urlParam = pageUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //传入拼接后的品牌名称
        String brandNameParam = pageBrandName(searchParam);
        model.addAttribute("brandNameParam",brandNameParam);
        //传入拼接后的平台属性
        List<Map<String,String>> propsParamList = pageProps(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        //传入拼接后的排序
        Map<String,Object> orderMap = pageOrder(searchParam);
        model.addAttribute("orderMap",orderMap);
        return "search/index";
    }

    private Map<String, Object> pageOrder(SearchParam searchParam) {
        Map<String,Object> map = new HashMap<>();
        String order = searchParam.getOrder();
        if(!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if(split.length == 2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        }else {
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;
    }

    private List<Map<String, String>> pageProps(SearchParam searchParam) {
        List<Map<String, String>> list = new ArrayList<>();
        String[] props = searchParam.getProps();
        if(props!=null&&props.length>0){
            for (String prop : props) {
                String[] split = prop.split(":");
                if(split.length == 3){
                    Map<String, String> map = new HashMap<>();
                    map.put("propertyKeyId",split[0]);
                    map.put("propertyValue",split[1]);
                    map.put("propertyKey",split[2]);
                    list.add(map);
                }
            }
        }
        return list;
    }

    private String pageBrandName(SearchParam searchParam) {
        if(!StringUtils.isEmpty(searchParam.getBrandName())){
            String[] split = searchParam.getBrandName().split(":");
            if(split.length == 2){
                return "品牌："+split[1];
            }
        }
        return "";
    }

    private String pageUrlParam(SearchParam searchParam) {
        StringBuilder urlParam = new StringBuilder();
        if(!StringUtils.isEmpty(searchParam.getKeyword())){
            urlParam.append("keyword=").append(searchParam.getKeyword());
        }
        if(!StringUtils.isEmpty(searchParam.getCategory1Id())){
            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if(!StringUtils.isEmpty(searchParam.getCategory2Id())){
            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if(!StringUtils.isEmpty(searchParam.getCategory3Id())){
            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
        }
        if (!StringUtils.isEmpty(searchParam.getBrandName())){
            if(!StringUtils.isEmpty(urlParam)){
                urlParam.append("&brandName=").append(searchParam.getBrandName());
            }
        }
        if (!StringUtils.isEmpty(searchParam.getProps())){
            for (String prop : searchParam.getProps()) {
                urlParam.append("&props=").append(prop);
            }
        }
        return "search.html?"+urlParam.toString();
    }

}
